//
//  FadeOutView.m
//  iddressbook
//
//  Created by 谌启亮 on 10-11-26.
//  Copyright 2010 Digisys Information. All rights reserved.
//

#import "FadeOutView.h"
#import <QuartzCore/QuartzCore.h>
#import "POP.h"


void showFadeOutView(NSString *text, BOOL success, NSTimeInterval d){
    FadeOutView *fadeOutView = [[FadeOutView alloc] initWithText:text isSuccess:success delay:d];
    [fadeOutView show];
}

void showFadeOutLongView(NSString *text, BOOL success, NSTimeInterval d){
    FadeOutView *fadeOutView = [[FadeOutView alloc] initLongViewWithText:text isSuccess:success delay:d];
    [fadeOutView show];
}

void showFadeOutHorizentalView(NSString *text, BOOL success, NSTimeInterval d){
    FadeOutView *fadeOutView = [[FadeOutView alloc] initWithHorizentalText:text isSuccess:success delay:d];
    [fadeOutView showHorizental];
}

void showFadeOutText(NSString *text, CGFloat offsetY,  NSTimeInterval interval) {
    FadeOutView *fadeOutView = [[FadeOutView alloc] initWithText:text offsetY:offsetY delay:interval];
    [fadeOutView show];
}

void showFadeoutImageWithText(NSString *text, UIImage *image, NSTimeInterval interval) {
    FadeOutView *fadeOutView = [[FadeOutView alloc] initWithText:text image:image delay:interval];
    [fadeOutView show];
}

void showFadeOutView_diamond(NSString *text0, NSString *text1, UIImage *image0, UIImage *image1, NSTimeInterval interval) {
    FadeOutView *fadeOutView = [[FadeOutView alloc] initWithText:text0 text1:text1 image:image0 diamondImage:image1 delay:interval];
    [fadeOutView show];
}

//void showFadeOutViewAuto(NSString *text, BOOL success, NSTimeInterval d){
//    if (isVertical) {
//        showFadeOutView(text, success, d);
//    }else {
//        showFadeOutHorizentalView(text, success, d);
//    }
//    
//}

#define degreesToRadian(x) (M_PI * (x) / 180.0)

@interface FadeOutView ()
{
    BOOL _horizentalEnabled;    //是否可以支持横屏
}

// diamond
@property (nonatomic ,strong) UIImageView * image0;
@property (nonatomic ,strong) UIImageView * image1;
@property (nonatomic ,strong) UILabel *label0;
@property (nonatomic ,strong) UILabel *label1;

@end

@implementation FadeOutView
@synthesize label,imageView;

- (id)initWithText:(NSString *)text offsetY:(CGFloat)offsetY delay:(NSTimeInterval)interval{
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]) {
        self.windowLevel = UIWindowLevelAlert;
        CGSize textSize = [text boundingRectWithSize:CGSizeMake(kScreenWidth, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} context:nil].size;
        
        int labelWidth = textSize.width+20;
        delay = interval;
        if (offsetY == 0) {
            offsetY = 380;
        }
        self.imageView = NULL;
        self.label = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth-labelWidth)/2, offsetY, labelWidth, 30)];
        label.numberOfLines = 1;
        label.backgroundColor = [UIColor blackColor];
        label.alpha = 0.75;
        label.layer.borderColor = [UIColor blackColor].CGColor;
        label.layer.cornerRadius = 5;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont boldSystemFontOfSize:13];
        label.text = text;
        label.adjustsFontSizeToFitWidth = YES;
        [self addSubview:label];
    }
    return self;
}

- (id)init{
    return [self initWithText:nil isSuccess:YES delay:1];
}

- (id)initWithText:(NSString*)text image:(UIImage*)image delay:(NSTimeInterval)d {
    if ((self = [super initWithFrame:CGRectMake(0, 0, 185, 40)])) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.75];
        self.layer.cornerRadius = 5;
        self.windowLevel = UIWindowLevelAlert;
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
        imageView.backgroundColor = [UIColor clearColor];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = image;
        imageView.autoresizingMask = UIViewContentModeLeft|UIViewContentModeRight;
        delay = d;
        //imageView.contentMode = UIViewContentModeCenter;
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 150, 40)];
        label.numberOfLines = 2;
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont boldSystemFontOfSize:15];
        label.text = text;
        label.adjustsFontSizeToFitWidth = YES;
        [self addSubview:imageView];
        [self addSubview:label];
    }
    return self;
}

- (id)initWithHorizentalText:(NSString*)text isSuccess:(BOOL)success delay:(NSTimeInterval)d {
    _horizentalEnabled = YES;   //支持横屏
    
        //横屏
//        NSLog(@"横 fade");

        if ((self = [super initWithFrame:CGRectMake(0, 0, 70, 280)])) {
            self.backgroundColor = [UIColor colorWithHex:@"273238"];
            self.layer.cornerRadius = 2;
            self.windowLevel = UIWindowLevelAlert;
            self.alpha = 0.9;
            delay = d;
            imageView.contentMode = UIViewContentModeCenter;
            self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 70, 280)];
            label.numberOfLines = 2;
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = ka9c8d7;
            label.font = [UIFont systemFontOfSize:16];
            label.text = text;
            label.adjustsFontSizeToFitWidth = YES;
            [self addSubviewHorizental:label];
        }
    return self;
}

- (id)initWithText:(NSString*)text isSuccess:(BOOL)success delay:(NSTimeInterval)d {
    if (![text isKindOfClass:[NSString class]]) {
        return nil;
    }
    //竖屏
    if ((self = [super initWithFrame:CGRectMake(0, 0, 280, 70)])) {
        self.backgroundColor = [UIColor colorWithHex:@"273238"];
        self.alpha = 0.9;
        self.layer.cornerRadius = 2;
        self.windowLevel = UIWindowLevelAlert;
        delay = d;
        imageView.contentMode = UIViewContentModeCenter;
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 260, 70)];
        label.numberOfLines = 2;
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = ka9c8d7;
        label.font = [UIFont systemFontOfSize:16];
        label.text = text;
        label.adjustsFontSizeToFitWidth = YES;
        [self addSubview:label];
    }

    return self;
}

- (id)initLongViewWithText:(NSString*)text isSuccess:(BOOL)success delay:(NSTimeInterval)d {
    if (![text isKindOfClass:[NSString class]]) {
        return nil;
    }
    //竖屏
    if ((self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)])) {
        self.backgroundColor = [UIColor colorWithHex:@"273238"];
        self.alpha = 0.9;
        self.layer.cornerRadius = 2;
        self.windowLevel = UIWindowLevelAlert;
        delay = d;
        imageView.contentMode = UIViewContentModeCenter;
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth, 70)];
        label.numberOfLines = 2;
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = ka9c8d7;
        label.font = [UIFont systemFontOfSize:16];
        label.text = text;
        label.adjustsFontSizeToFitWidth = YES;
        [self addSubview:label];
    }
    
    return self;
}


//钻石
- (id)initWithText:(NSString *)text0 text1:(NSString *)text1 image:(UIImage *)image0 diamondImage:(UIImage *)image1 delay:(NSTimeInterval)d{
    
    if ((self = [super initWithFrame:CGRectMake(0, 0, 290, 130)])) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.75];
        self.layer.cornerRadius = 5;
        self.windowLevel = UIWindowLevelAlert;
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 140, 60)];
        imageView.backgroundColor = [UIColor clearColor];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = image0;
        imageView.autoresizingMask = UIViewContentModeLeft|UIViewContentModeRight;
        delay = d;
        //imageView.contentMode = UIViewContentModeCenter;
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 120, 48)];
        label.numberOfLines = 2;
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont boldSystemFontOfSize:12];
        label.text = text1;
        label.adjustsFontSizeToFitWidth = YES;
        [self addSubview:imageView];
        [self addSubview:label];
    }
    return self;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)fadeOut{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.hidden = YES;
//        [self removeFromSuperview];
    }];
}


- (void)showHorizental{
    self.hidden = NO;
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fadeOut) object:nil];
    [self performSelector:@selector(fadeOut) withObject:nil afterDelay:delay];

    //不要用宏，宏不能控制横竖屏的时候自动切换
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    self.center = CGPointMake(screenHeight / 2 , screenWidth / 2);
}

- (void)show {
    self.hidden = NO;
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fadeOut) object:nil];
    [self performSelector:@selector(fadeOut) withObject:nil afterDelay:delay];

    //不要用宏，宏不能控制横竖屏的时候自动切换
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    self.center = CGPointMake(screenWidth / 2 , screenHeight / 2);
    //spring动画
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.2, 0.2)];
    animation.toValue =[NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
    animation.springBounciness = 10;
    [self.layer pop_addAnimation:animation forKey:@"springAni"];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fadeOut) object:nil];
    [self fadeOut];
}

- (void)addSubviewHorizental:(UIView *)view {
    [super addSubview:view];
        //横屏
    float angle;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeRight) {
        angle = 90;
    }else if(orientation == UIInterfaceOrientationLandscapeLeft){
        angle = 270;
    }else {
        angle = 0;
    }
    view.transform = CGAffineTransformMakeRotation(degreesToRadian(angle));
    view.frame     = CGRectOffset(self.frame, 0, 0);
}


- (void)dealloc {
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

@end
